<script>
function num(num)
{
        if(num == ""){
                return false;
        }
        var pattern = /^\d{3}$/;
        return pattern.test(num);
}
$(document).ready(function(){
	$("#mod_id").change(function(){
       var mod_id=$(this).val();
       if(mod_id==""){
    	   var str="<font color='red'>请选择型号！</font>";
           $("#mod_id_notice").html('');
           $("#mod_id_notice").html(str);
           return false;
       }else{
    	   $("#mod_id_notice").html('');
       }
	})
	$("#level_id").change(function(){
       var mod_id=$(this).val();
       if(mod_id==""){
    	   var str="<font color='red'>请选择密级！</font>";
           $("#level_notice").html('');
           $("#level_notice").html(str);
           return false;
       }else{
    	   $("#mod_id_notice").html('');
       }
	})
	
	$("#name").blur(function(){
       var name = $(this).val();
       if(name==""){
    	   var str="<font color='red'>&nbsp;名称不能空！</font>";
           $("#name_notice").html('');
           $("#name_notice").html(str);
       }else{
    	   $("#name_notice").html('');
       }
	})
    $("#codes").keyup(function(){
        var codes = $(this).val();
        var mod_id=$("#mod_id").val();
        if(mod_id==""){
        	var str="<font color='red'>&nbsp;请选择型号！</font>";
            $("#mod_id_notice").html('');
            $("#mod_id_notice").html(str);
            return false;
        }
        if(codes==""){
      	   var str="<font color='red'>&nbsp;编号不能空！</font>";
             $("#codes_notice").html('');
             $("#codes_notice").html(str);
         }else if(!num(codes)){
        	 var str="<font color='red'>&nbsp;编号格式：001,002,003...！</font>";
             $("#codes_notice").html('');
             $("#codes_notice").html(str);
         }else{
	        $.ajax({
		    	   type: "POST",
		    	   url: "<?php echo base_url();?>model/auth",
		    	   data: "codes="+codes+"&mod_id="+mod_id+"&type=category",
		    	   success: function(msg){
		    		    if(msg>=1){
	                     var str="<font color='red'>&nbsp;此编号已经存在，请更改！</font>";
	                 }else{
	                 	var str="<font color='green'>&nbsp;此编号可使用！</font>";
	                 }
	                 $("#codes_notice").html('');
	                 $("#codes_notice").html(str);
		    	   }
		    	}); 
         }
    })
    
    $("#codes").blur(function(){
        var codes = $(this).val();
        var mod_id=$("#mod_id").val();
        if(mod_id==""){
        	var str="<font color='red'>&nbsp;请选择型号！</font>";
            $("#mod_id_notice").html('');
            $("#mod_id_notice").html(str);
            return false;
        }
        if(codes==""){
        	   var str="<font color='red'>&nbsp;编号不能空！</font>";
               $("#codes_notice").html('');
               $("#codes_notice").html(str);
           }else if(!num(codes)){
        	 var str="<font color='red'>&nbsp;编号格式：001,002,003...！</font>";
             $("#codes_notice").html('');
             $("#codes_notice").html(str);
         }else{
	        $.ajax({
		    	   type: "POST",
		    	   url: "<?php echo base_url();?>model/auth",
		    	   data: "codes="+codes+"&mod_id="+mod_id+"&type=category",
		    	   success: function(msg){
		    		   if(msg>=1){
	                    var str="<font color='red'>&nbsp;此编号已经存在，请更改！</font>";
	                }else{
	                	   var str="<font color='green'>&nbsp;此编号可使用！</font>";
	                }
	                $("#codes_notice").html('');
	                $("#codes_notice").html(str);
		    	   }
		    	}); 
           }
    })

    $("#submit").click(function(){
       var mod_id = $("#mod_id").val();
       var codes = $("#codes").val();
       var name = $("#name").val();
       var level = $("#level").val();
       if(mod_id==""){
    	   var str="<font color='red'>请选择型号！</font>";
           $("#mod_id_notice").html('');
           $("#mod_id_notice").html(str);
           return false;
       }
      if(codes==""){
    	   var str="<font color='red'>&nbsp;编号不能空！</font>";
           $("#codes_notice").html('');
           $("#codes_notice").html(str);
           return false;
      }else if(codes=="000"){
    	  var str="<font color='red'>&nbsp;编号不能为：000！</font>";
          $("#codes_notice").html('');
          $("#codes_notice").html(str);
          return false;
      }

      if(name==""){
    	  var str="<font color='red'>&nbsp;名称不能空！</font>";
          $("#name_notice").html('');
          $("#name_notice").html(str);
          return false;
      }if(level==""){
    	  var str="<font color='red'>&nbsp;编号不能空！</font>";
          $("#level_notice").html('');
          $("#level_notice").html(str);
          return false;
      }else{
    	  $("#mod_id_notice").html('');
    	  $("#codes_notice").html('');
    	  $("#name_notice").html('');
    	  $("#level_notice").html('');
       }
    })     
})
</script>
<div id="center-column">
			<div class="top-bar">
				<a href="<?php echo  base_url();?>model/category_add" class="button">新增</a>
				<h1>Contents</h1>
				<div class="breadcrumbs"><a href="#">首页</a> /<?php echo anchor('model/category_add','添加资料类别');?></div>
			</div>
			
		  <div class="table add_table">
				
				
				<table class="listing form" cellpadding="0" cellspacing="0">
				  <?php if(isset($id)){echo form_open('model/category_edit/'.$id);}else{echo form_open('model/category_add');}?>
					<tr>
						<th  colspan="2"  align="center"><?php if(isset($id)){?>编辑资料种类<?php }else{?>新增资料种类<?php }?></th>
					</tr>
					<tr>
						<td class="first" width="172"><strong>型号：</strong></td>
						<td class="last"><select name="mod_id" id="mod_id">
						                   <option value="">请选择型号</option>
						                   <?php foreach($mod as $k=>$val):?>
						                     <option value="<?php echo $val->mod_id;?>" <?php if(isset($id)){ if($val->mod_id==$category->mod_id){?>selected<?php } }?>><?php echo $val->name;?></option>
						                   <?php endforeach;?>
						                 </select><span id="mod_id_notice"></span></td>
					</tr>
					<tr class="bg">
						<td class="first"><strong>资料编号：</strong></td>
						<td class="last"><input type="text" class="text" name="codes" id="codes" value="<?php if(isset($id)){echo $category->codes;}?>" /><span id="codes_notice"></span></td>
					</tr>
					<tr>
						<td class="first""><strong>资料名称：</strong></td>
						<td class="last"><input type="text" class="text" name="name" id="name" value="<?php if(isset($id)){echo $category->name;}?>" /><span id="name_notice"></span></td>
					</tr>
					<tr class="bg">
						<td class="first""><strong>密级：</strong></td>
						<td class="last"><select name="level" id="level">
						                 <option value="">请选择密级</option>
						                  <?php foreach($level_data as $k=>$val):?>
						                     <option value="<?php echo $k;?>"  <?php if(isset($id)){ if($category->level==$k){?>selected=true<?php }}?>><?php echo $val;?></option>
						                  <?php endforeach;?> 
						                 </select><span id="level_notice"></span></td>
					</tr>
					<tr>
						<td colspan="2" style="text-align:center;"><input type="submit" value="提交" class="button" id="submit"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="返回" class="button" onclick="location.href='<?php echo site_url()?>model/category'" /></td>			
					</tr>
				</form>	
				</table>
	        <p>&nbsp;</p>
		  </div>
		</div>
	</div>
	<div id="footer"></div>
</div>
</body>
</html>
